﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net;
using ZP.Data;
using ZP.Service;
using Kwan.web;
using Kwan.service;

namespace ZP.Web.Admin.Resume
{
    public partial class Default : System.Web.UI.Page
    {
        BaseService<TResume, TResume, string> facade = new BaseService<TResume, TResume, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void BtnAddInfo_Click(object sender, EventArgs e)
        {
            NewEditWindow();
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            List<TResume> list = JSON.Deserialize<List<TResume>>(obj);
            if (list.Count > 0)
            {
                foreach (TResume t in list)
                {
                    facade.EntityDAO.Delete(t);
                }
                Store1.DataBind();
            }
        }

        [DirectMethod]
        public void ChangeAllState(string obj, string cmd)
        {
            List<TResume> list = JSON.Deserialize<List<TResume>>(obj);
            if (list.Count > 0)
            {
                foreach (TResume tt in list)
                {
                    switch (cmd)
                    {
                        case "ScaleTrue": tt.Scale = "1"; break;
                        case "ScaleFalse": tt.Scale = "0"; break;
                    }
                    facade.EntityDAO.Save(tt);
                    tt.ModifiedDate = DateTime.Now.ToShortDateString();
                }
                Store1.DataBind();
            }
            else
            {
                Notification notify = ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
        }

        [DirectMethod]
        public void ChangeState(string id,string cmd)
        {
            TResume tt = facade.EntityDAO.LoadById(id);
            switch(cmd)
            {
                case "Scale": tt.Scale = tt.Scale == "1" ? "0" : "1"; break;
            }
            facade.EntityDAO.Save(tt);
            Store1.DataBind();
        }

        [DirectMethod]
        public void EditInfo(string id)
        {
            SetEditWindow(facade.EntityDAO.LoadById(id));
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteInfo(string id)
        {
            facade.DeleteById(id);
            Store1.DataBind();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());
            
            //搜索项
            ps.Set("txtSearch", txtSearch.Text);

            facade.ViewDAO.QueryBuilder = new ResumeQuery();
            List<TResume> ListView = facade.LoadListData(ps, e.Start, e.Limit);

            e.Total = Convert.ToInt32(facade.TotalCount(ps));
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void NewEditWindow()
        {
            txtID.Text = string.Empty;
            txtHot.Checked = false;
            txtScale.Checked = true;
			txtAddress.Text = string.Empty;
			txtBirth.Text = string.Empty;
			txtBorn.Text = string.Empty;
			txtClick.Text = string.Empty;
			txtControl.Text = string.Empty;
			txtEmail.Text = string.Empty;
			txtMatch.Text = string.Empty;
			txtModifiedDate.Text = string.Empty;
			txtName.Text = string.Empty;
			txtPhone.Text = string.Empty;
			txtPurpose.Text = string.Empty;
			txtResumeName.Text = string.Empty;
			txtSex.Text = string.Empty;
			txtWorkYear.Text = string.Empty;
        }

        protected void SetEditWindow(TResume tt)
        {
            txtID.Text = tt.id.ToString();
            txtScale.Checked = tt.Scale == "1";
			txtAddress.Text = tt.Address;
			txtBirth.Text = tt.Birth;
			txtBorn.Text = tt.Born;
			txtClick.Text = tt.Click;
			txtControl.Text = tt.Control;
			txtEmail.Text = tt.Email;
			txtMatch.Text = tt.Match;
			txtModifiedDate.Text = tt.ModifiedDate;
			txtName.Text = tt.Name;
			txtPhone.Text = tt.Phone;
			txtPurpose.Text = tt.Purpose;
			txtResumeName.Text = tt.ResumeName;
			txtSex.Text = tt.Sex;
			txtWorkYear.Text = tt.WorkYear;
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            TResume tt = !String.IsNullOrEmpty(txtID.Text) ? facade.EntityDAO.LoadById(txtID.Text) : new TResume();

			tt.Address = txtAddress.Text;
			tt.Birth = txtBirth.Text;
			tt.Born = txtBorn.Text;
			tt.Click = txtClick.Text;
			tt.Control = txtControl.Text;
			tt.Email = txtEmail.Text;
			tt.Match = txtMatch.Text;
			tt.ModifiedDate = txtModifiedDate.Text;
			tt.Name = txtName.Text;
			tt.Phone = txtPhone.Text;
			tt.Purpose = txtPurpose.Text;
			tt.ResumeName = txtResumeName.Text;
			tt.Sex = txtSex.Text;
			tt.WorkYear = txtWorkYear.Text;
            tt.Scale = txtScale.Checked ? "1" : "0";

            facade.EntityDAO.Save(tt);

            Store1.DataBind();
            EditWindow.Hide();
        }
    }
}